//
//  SinaShareMethod.m
//  Growth
//
//  Created by 洪波 陈 on 7/26/12.
//  Copyright (c) 2012 MEETStudio. All rights reserved.
//

#import "SinaShareMethod.h"


#define kWBAlertViewLogOutTag 100
#define kWBAlertViewLogInTag  101

@interface SinaShareMethod (Private)

@end

@implementation SinaShareMethod

@synthesize weiBoEngine;



static SinaShareMethod *_shareMethod = nil;

+ (SinaShareMethod *)sharedSinaShareMethod{
	if (!_shareMethod) {
		_shareMethod = [[SinaShareMethod alloc] initWith:nil];
		
	}
	
	return _shareMethod;
}

- (void)dealloc
{
    [weiBoEngine setDelegate:nil];
    [weiBoEngine release], weiBoEngine = nil;
    [super dealloc];
}


- (id)initWith:(UIViewController *)rootViewController
{
    if (self = [super init]) {
        WBEngine *engine = [[WBEngine alloc] initWithAppKey:kWBSDKDemoAppKey appSecret:kWBSDKDemoAppSecret];
        [engine setRootViewController:rootViewController];
        [engine setDelegate:self];
        [engine setRedirectURI:@"http://www.lanbaoo.com"];
        [engine setIsUserExclusive:NO];
        self.weiBoEngine = engine;
        [engine release];
    }

    return self;
}

- (void)setWBEngineRootViewController:(UIViewController *)rootViewController
{
    [weiBoEngine setRootViewController:rootViewController];
}

- (void)sendWeiBoWithText:(NSString *)text image:(UIImage *)image
{
    NSString *str = [NSString stringWithFormat:@"【印象宝贝分享】%@",text];
    
    CLog(@"Sina share %@", str);
    
    [self.weiBoEngine sendWeiBoWithText:str image:image];
}


#pragma mark - UIAlertViewDelegate Methods

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    //需要做其他处理，登陆失败给出delegate通知，不能完成分享
    if (alertView.tag == kWBAlertViewLogInTag)
    {
       //登陆成功
    }
    else if (alertView.tag == kWBAlertViewLogOutTag)
    {
       //登出成功
    }
}

#pragma mark - WBEngineDelegate Methods

#pragma mark Authorize

- (void)engineAlreadyLoggedIn:(WBEngine *)engine
{

    if ([engine isUserExclusive])
    {
        UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:nil 
                                                           message:@"请先登出新浪微博！" 
                                                          delegate:nil
                                                 cancelButtonTitle:@"确定" 
                                                 otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }
}

- (void)engineDidLogIn:(WBEngine *)engine
{

    UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:nil 
													   message:@"新浪微博登录成功！" 
													  delegate:self
											 cancelButtonTitle:@"确定" 
											 otherButtonTitles:nil];
    [alertView setTag:kWBAlertViewLogInTag];
	[alertView show];
	[alertView release];
}

- (void)engine:(WBEngine *)engine didFailToLogInWithError:(NSError *)error
{
    
    NSLog(@"didFailToLogInWithError: %@", error);
    UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:nil 
													   message:@"新浪微博登录失败！" 
													  delegate:nil
											 cancelButtonTitle:@"确定" 
											 otherButtonTitles:nil];
	[alertView show];
	[alertView release];
}

- (void)engineDidLogOut:(WBEngine *)engine
{
    CLog(@"Sina weibo log out success!");
//    UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:nil 
//													   message:@"登出成功！" 
//													  delegate:self
//											 cancelButtonTitle:@"确定" 
//											 otherButtonTitles:nil];
//    [alertView setTag:kWBAlertViewLogOutTag];
//	[alertView show];
//	[alertView release];
}

- (void)engineNotAuthorized:(WBEngine *)engine
{
    [weiBoEngine logIn];
}

- (void)engineAuthorizeExpired:(WBEngine *)engine
{
    UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:nil 
													   message:@"请重新登录新浪微博！" 
													  delegate:nil
											 cancelButtonTitle:@"确定" 
											 otherButtonTitles:nil];
	[alertView show];
	[alertView release];
}


- (void)engine:(WBEngine *)engine requestDidFailWithError:(NSError *)error
{
    CLog(@"Sina request error %@", error);
}

- (void)engine:(WBEngine *)engine requestDidSucceedWithResult:(id)result
{
    CLog(@"Sina request success %@", result);
    [LoadingView showImage:[UIImage imageNamed:@"wangluo_success_iphone.png"] info:@"新浪微博分享成功" duration:DEFAULT_LOADING_TIME /2];

}

@end
